package com.insea.webapp.action;

import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.insea.model.Acte;
import com.insea.model.LabelValue;
import com.insea.model.Patient;
import com.insea.model.User;
import com.insea.Constants;

public class ActeAction extends BaseAction{
	
	private List actes;
	private String id;
	private Acte acte;
	public String query;
	
	public String list(){
		if(query == null || "".equals(query)){
			actes = acteManager.getAll();	
		}else{
			actes = acteManager.getActesByName(query);
		}
		
		return SUCCESS;
	}
	
	public String edit(){
		if (id != null) {
	        acte = acteManager.get(Long.valueOf(id));
	    } else {
	        acte = new Acte();
	    }
		getRequest().getSession().setAttribute("acte", acte);
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		User user = (User) auth.getPrincipal();
		
		log.debug("role is : " + user.getRoleList());
		
		for (LabelValue tmp : user.getRoleList()){
			
			if(tmp.getValue().equals(Constants.ROLE_MEDECIN)){
				return INPUT;			
			}
			if(tmp.getValue().equals(Constants.ROLE_AGENTBAF)){
				return SUCCESS;			
			}
		}
		return ERROR;
	}
	
	public String save(){
		Acte updatedActe = (Acte) getRequest().getSession().getAttribute("acte");

		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		User user = (User) auth.getPrincipal();
		for (LabelValue tmp : user.getRoleList()){
			
			if(tmp.getValue().equals(Constants.ROLE_MEDECIN)){
				if(acte.getAbrevNomActe()==null || "".equals(acte.getTarifActe())){
					String key ="code.error";
					saveMessage(getText(key));
					acte = (Acte) getRequest().getSession().getAttribute("acte");
					return ERROR;
				}
				updatedActe.setAbrevNomActe(acte.getAbrevNomActe());
			}
			if(tmp.getValue().equals(Constants.ROLE_AGENTBAF)){
							
				if(acte.getTarifActe()==null || "".equals(acte.getTarifActe())){
					String key ="tarif.error";
					saveMessage(getText(key));
					acte = (Acte) getRequest().getSession().getAttribute("acte");
					return ERROR;
				}
				
				try{
					Float tarif = Float.parseFloat(acte.getTarifActe());
				}catch(Exception ex){
					String key ="tarif.wrong";
					saveMessage(getText(key));
					acte = (Acte) getRequest().getSession().getAttribute("acte");
					return ERROR;
				}
				updatedActe.setTarifActe(acte.getTarifActe());
			}
		}
		
		acteManager.save(updatedActe);
		String key ="acte.updated";
		saveMessage(getText(key));
		return SUCCESS;
	}
	
	
	// Getters and Setters
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Acte getActe() {
		return acte;
	}
	public void setActe(Acte acte) {
		this.acte = acte;
	}
	public List getActes() {
		return actes;
	}
	public void setQuery(String query) {
		this.query = query;
	}

}
